import Vue from 'vue'
import VueRouter from 'vue-router'
import ListArticle from '../views/ListArticle.vue'
import CreateArticle from '../views/CreateArticle.vue'
import EditArticle from '../views/EditArticle.vue'
import UserLogin from '../components/Login.vue'
import UserApp from '../App.vue'

import AdminUsers from '../components/admin.vue'
import UserRegister from '../components/register.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    /* redirect: '/articles/list', */
    redirect: '/user/login'  
  },
  {
    path: '/user/regiter',
    name: '/user-regiter',
    component: UserRegister,
  },
  {
    path: '/users/admin',
    name: '/users-admin',
    component: AdminUsers,
  },
  {
    path: '/user/index',
    name: 'index',
    component: UserApp,
    children:[
      {
        path: '/articles/create',
        name: 'create-article',
        component: CreateArticle
      },
      {
        path: '/articles/list',
        name: 'list-article',
        component: ListArticle,
      },
      {
        path: '/articles/edit/:id',
        name: 'edit-article',
        component: EditArticle
      } 
    ]
  },
  {
    path: '/user/login',
    name: 'login',
    component: UserLogin
  },
 
]

const router = new VueRouter({
  routes
})


//路由守卫
router.beforeEach((to ,from ,next ,) =>{
  
  const isToken = localStorage.UserToken ? true:false
  //判断跳转页面的url路径
  if( to.path == '/user/login' || to.path == '/user/register'){
    //跳转登陆页面或注册页面；放行
    next()
  }else{
    isToken ? next():next( '/user/login')
  }
})

export default router
